//
//  STStingTableViewCell.m
//  STBasicsApp
//
//  Created by jerry on 2019/5/6.
//  Copyright © 2019 jerry. All rights reserved.
//

#import "STStingTableViewCell.h"
//#import "UIImageView+STSD.h"

#define ICON_WIDTH  22
#define ICON_HEIGHT 22
#define LEFT_SPACE  15
#define RIGHT_SPACE 10

#define ARROW_WIDTH  20
#define ARROW_HEIGHT 20

#define SWITCH_WIDTH   55
#define SWITCH_HEIGHT  35

#define TITLE_WIDTH   120

@interface STStingTableViewCell()

@property(nonatomic,strong) UIImageView * iconIv;
@property(nonatomic,strong) UILabel * leftLabel;
@property(nonatomic,strong) UILabel * rightLabel01;
@property(nonatomic,strong) UILabel * rightLabel02;
@property(nonatomic,strong) UIImageView * arrowIv;
@property(nonatomic,strong) UISwitch * switchBtn;

@property(nonatomic,strong) UIView * topLine;
@property(nonatomic,strong) UIView * bottomLine;

@end

@implementation STStingTableViewCell

+(instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"STStingTableViewCell";
    STStingTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[STStingTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self showView];
    }
    return self;
}

-(void)showView{
    self.dk_backgroundColorPicker = DKColorPickerWithKey(TABLECELLBG);
}

#pragma mark - set/get

-(UIImageView *)iconIv{
    if(!_iconIv){
        _iconIv = [[UIImageView alloc]init];
        _iconIv.backgroundColor = kClearColor;
        [self addSubview:_iconIv];
    }
    return _iconIv;
}

-(UILabel *)leftLabel{
    if(!_leftLabel){
        _leftLabel = [[UILabel alloc]init];
        _leftLabel.dk_textColorPicker = DKColorPickerWithKey(SETTINGMAINTEXT);
        _leftLabel.font = kFont(18.0);
        _leftLabel.numberOfLines = 1;
        [self addSubview:_leftLabel];
    }
    return _leftLabel;
}

-(UILabel *)rightLabel01{
    if(!_rightLabel01){
        _rightLabel01 = [[UILabel alloc]init];
        _rightLabel01.dk_textColorPicker = DKColorPickerWithKey(SETTINGRIGHTTEXT);
        _rightLabel01.textAlignment = NSTextAlignmentRight;
        _rightLabel01.font = kFont(14.0);
        _rightLabel01.numberOfLines = 1;
        [self addSubview:_rightLabel01];
    }
    return _rightLabel01;
}

-(UILabel *)rightLabel02{
    if(!_rightLabel02){
        _rightLabel02 = [[UILabel alloc]init];
        _rightLabel02.dk_textColorPicker = DKColorPickerWithKey(SETTINGRIGHTTEXT);
        _rightLabel02.textAlignment = NSTextAlignmentRight;
        _rightLabel02.font = kFont(12.0);
        _rightLabel02.numberOfLines = 1;
        [self addSubview:_rightLabel02];
    }
    return _rightLabel02;
}

-(UIImageView *)arrowIv{
    if(!_arrowIv){
        _arrowIv = [[UIImageView alloc]init];
        _arrowIv.image = [UIImage imageNamed:@"st_icon_right_arrow_n"];
        [self addSubview:_arrowIv];
    }
    return _arrowIv;
}

-(UISwitch *)switchBtn{
    if (!_switchBtn) {
        _switchBtn = [[UISwitch alloc]init];
        [_switchBtn addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_switchBtn];
    }
    return _switchBtn;
}

-(UIView *)topLine{
    if(!_topLine){
        _topLine = [[UIView alloc]init];
        _topLine.dk_backgroundColorPicker = DKColorPickerWithKey(SETTINGSEPARATOR);
        [self addSubview:_topLine];
    }
    return _topLine;
}

-(UIView *)bottomLine{
    if(!_bottomLine){
        _bottomLine = [[UIView alloc]init];
        _bottomLine.dk_backgroundColorPicker = DKColorPickerWithKey(SETTINGSEPARATOR);
        [self addSubview:_bottomLine];
    }
    return _bottomLine;
}

#pragma mark -

- (void)setItemModel:(STSetingItemModel *)itemModel{
    _itemModel = itemModel;
    if (itemModel) {
        [self updateItemView:itemModel.itemType];
        
        NSString * leftIcon = itemModel.leftIcon ? itemModel.leftIcon :@"";
        [self.iconIv setImage:[UIImage imageNamed:leftIcon]];
        
        NSString * leftTitle = itemModel.leftTitle ? itemModel.leftTitle :@"";
        self.leftLabel.text = ASLocalizedString(leftTitle);
        
        NSString * rightTitle01 = itemModel.rightTitle01 ? itemModel.rightTitle01 :@"";
        self.rightLabel01.text = ASLocalizedString(rightTitle01);
        
        NSString * rightTitle02 = itemModel.rightTitle02 ? itemModel.rightTitle02 :@"";
        self.rightLabel02.text = ASLocalizedString(rightTitle02);
        
    }
}

- (void)updateItemView:(STSetingItemType)itemType{
    switch (itemType) {
        case STSetingItemTypeNormal:
        {
            //普通类型
            [self.iconIv mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(ICON_WIDTH, ICON_HEIGHT));
                make.left.equalTo(self.mas_left).offset(LEFT_SPACE);
                make.centerY.equalTo(self.mas_centerY);
            }];
            
            [self.arrowIv mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(ARROW_WIDTH, ARROW_WIDTH));
                make.right.equalTo(self.mas_right).offset(-RIGHT_SPACE);
                make.centerY.equalTo(self.mas_centerY);
            }];
            
            [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.iconIv.mas_right).offset(5);
                make.width.equalTo(@TITLE_WIDTH);
                make.centerY.equalTo(self.mas_centerY);
            }];
            
            [self.rightLabel01 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.arrowIv.mas_left).offset(-5);
                make.width.mas_greaterThanOrEqualTo(80);
                make.centerY.equalTo(self.mas_centerY);
            }];
            
            [self.rightLabel02 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.rightLabel01.mas_left).offset(-1);
                //make.left.equalTo(self.leftLabel.mas_right).offset(5);
                make.centerY.equalTo(self.mas_centerY);
            }];
            
            [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.mas_left);
                make.right.equalTo(self.mas_right);
                make.bottom.equalTo(self.mas_bottom);
                make.height.equalTo(@1);
            }];
            
        }
            break;
        case STSetingItemTypeSwitch:
        {
            
            [self.iconIv mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(ICON_WIDTH, ICON_HEIGHT));
                make.left.equalTo(self.mas_left).offset(LEFT_SPACE);
                make.centerY.equalTo(self.mas_centerY);
            }];
            
            [self.switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(SWITCH_WIDTH, SWITCH_HEIGHT));
                make.right.equalTo(self.mas_right).offset(-RIGHT_SPACE);
                make.centerY.equalTo(self.mas_centerY).offset(1);
            }];

            [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.iconIv.mas_right).offset(5);
                make.right.equalTo(self.switchBtn.mas_left).offset(-5);
                make.centerY.equalTo(self.mas_centerY);
            }];
            
            [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.mas_left);
                make.right.equalTo(self.mas_right);
                make.bottom.equalTo(self.mas_bottom);
                make.height.equalTo(@1);
            }];

            
            
            
        }
            break;
        default:
            break;
    }
}

+ (CGFloat)getCellHeightWithType:(STSetingItemType)itemType{
    CGFloat height = 44.0f;
    
    switch (itemType) {
        case STSetingItemTypeNormal:
            height = 44.0f;
            break;
        case STSetingItemTypeSwitch:
            height = 44.0f;
            break;
        default:
            break;
    }
    
    return height;
}

#pragma mark - Action
-(void)switchAction:(UISwitch *)sender{
    if (self.switchChangeBlock) {
        self.switchChangeBlock(sender.on);
    }
}

#pragma mark -

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

@end
